import {
    FETCH_TIMER_REQUEST,
    FETCH_TIMER_SUCCESS,
    SAVE_TIMER_FAIl,
    SAVE_TIMER_REQUEST,
    SAVE_TIMER_SUCCESS
} from "./redux-actions";

const initialState={
    timers:[],
    isLoading:false,
    saveStatus: 'READY',
    timer: {
      title: '',
      project: '',
      id: '',
      elapsed: null,
      runningSince: null,
    },
}

export function reducer(state=initialState,action){
    switch (action.type) {
        case FETCH_TIMER_REQUEST:
            return Object.assign({},state,{
                isLoading: true
            });
        case FETCH_TIMER_SUCCESS:
            return Object.assign({},state,{
                isLoading: false,
                timer:action.timer
            });
        case SAVE_TIMER_REQUEST:
            return Object.assign({}, state,{
                saveStatus: 'SAVING'
            });
        case SAVE_TIMER_SUCCESS:
            return Object.assign({}, state,{
                saveStatus: 'SUCCESS',
                timers:action.timer,
                timer:{
                    title: '',
                    project: '',
                    id: '',
                    elapsed: null,
                    runningSince: null,
                }
            });
        case SAVE_TIMER_FAIl:
            return Object.assign({}, state,{
                saveStatus: 'ERROR'
            });
        default:
            return state;
    }
}